import axios, { AxiosResponse, AxiosRequestConfig, Axios } from 'axios'
import { ResultEnum, ModuleTypeEnum } from '@/enums/httpEnum'
import { PageEnum, ErrorPageNameMap } from '@/enums/pageEnum'
import { StorageEnum } from '@/enums/storageEnum'
import { axiosPre } from '@/settings/httpSetting'
import { SystemStoreEnum, SystemStoreUserInfoEnum } from '@/store/modules/systemStore/systemStore.d'
import { redirectErrorPage, getLocalStorage, routerTurnByName, isPreview, JSONParse, getCookie, setCookie } from '@/utils'
import { fetchAllowList } from './axios.config'
import includes from 'lodash/includes'
import { cookieEnum } from '@/enums/cookieEnum'

export interface MyResponseType<T> {
  code: ResultEnum
  data: T
  message: string
}

export interface MyRequestInstance extends Axios {
  <T = any>(config: AxiosRequestConfig): Promise<MyResponseType<T>>
}

export interface IObj {
  [key: string]: string
  token: string
}

const axiosInstance = axios.create({
  // baseURL: `${import.meta.env.PROD ? import.meta.env.VITE_PRO_PATH : ''}${axiosPre}`,
  baseURL: `${import.meta.env.VITE_APP_BASE_API}${axiosPre}`,
  timeout: ResultEnum.TIMEOUT
}) as unknown as MyRequestInstance

axiosInstance.interceptors.request.use(
  (config: AxiosRequestConfig) => {
    // 白名单校验
    if (includes(fetchAllowList, config.url)) return config
    // 获取 token
    // const info = getLocalStorage(StorageEnum.GO_SYSTEM_STORE)
    let info = getCookie(cookieEnum.COOKIE_TOKEN)
    // if(!info){
    //   const href = window.location.href
    //   let arr = href.split('?')
    //   let params = arr[1].split('&')
    //   let obj = {} as IObj
    //   for (let i = 0; i < params.length; i++) {
    //     let param = params[i].split('=')
    //     obj[param[0]] = param[1]
    //   }
    //   let newArr = obj.token.split('#')
  
    //   const newToken = newArr[0]
    //   console.log('newToken', newToken)
    //   setCookie(cookieEnum.COOKIE_TOKEN,newToken,7)
    //   info = getCookie(cookieEnum.COOKIE_TOKEN)
    // }
    
    // 重新登录
    if (!info) {
      routerTurnByName(PageEnum.BASE_LOGIN_NAME)
      return config
    }
    // const userInfo = info[SystemStoreEnum.USER_INFO]
    // const ruoyiTocken = 'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjEwNGFhODExLTljOWYtNGI0NC04OGNlLTdhZjU4N2Y5ZTU2MiIsInVzZXJuYW1lIjoiYWRtaW4ifQ.KkpbxEcsW42DWi9LtsnI7dXDwmBTnQs0V409Lam06S0czERweqY4gSskx7SDhp-oO8JOXDpUcmBIHJZV2JCshg'
    config.headers = {
      ...config.headers,
      ['Authorization']: 'Bearer ' + info
      // [userInfo[SystemStoreUserInfoEnum.TOKEN_NAME] || 'token']: userInfo[SystemStoreUserInfoEnum.USER_TOKEN] || ''
    }
    return config
  },
  (err: AxiosRequestConfig) => {
    Promise.reject(err)
  }
)

// 响应拦截器
axiosInstance.interceptors.response.use(
  (res: AxiosResponse) => {
    // 预览页面错误不进行处理
    if (isPreview()) {
      return Promise.resolve(res.data)
    }
    const { code } = res.data as { code: number }

    if (code === undefined || code === null) return Promise.resolve(res.data)

    // 成功
    if (code === ResultEnum.SUCCESS) {
      return Promise.resolve(res.data)
    }

    // 登录过期
    if (code === ResultEnum.TOKEN_OVERDUE) {
      window['$message'].error(window['$t']('http.token_overdue_message'))
      routerTurnByName(PageEnum.BASE_LOGIN_NAME)
      return Promise.resolve(res.data)
    }

    // 固定错误码重定向
    if (ErrorPageNameMap.get(code)) {
      redirectErrorPage(code)
      return Promise.resolve(res.data)
    }

    // 提示错误
    (res.data as any).msg && window['$message'].error(window['$t']((res.data as any).msg))
    return Promise.resolve(res.data)
  },
  (err: AxiosResponse) => {
    Promise.reject(err)
  }
)

export default axiosInstance
